/*****************************************************************************
 * dx100.c
 *
 * Copyright (C) 2020 Jeasonvor 1101627719@qq.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

/*****************************************************************************
 * Included Files
 ****************************************************************************/
#include "plum_api.h"
#include "thirdparty/modbus/modbus.h"
#include "hardware/vfd/vfd.h"
/*****************************************************************************
 * Trace Definitions
 ****************************************************************************/
#define LOG_RUN_LEVEL LOG_LEVEL_DEBUG
#define LOG_MODULE    "vfd.dx100"
#include "thirdparty/log/log.h"
/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

#define DEV_DX100_REG_CTL_FRE_IDX 0
#define DEV_DX100_REG_STA_FRE_IDX 1
#define DEV_DX100_REG_CTL_RUN_IDX 2
#define DEV_DX100_REG_STA_RUN_IDX 3

/*****************************************************************************
 * Private Types
 ****************************************************************************/
typedef struct {
    plum_u16 addr;
    plum_u16 value;
} dx100_reg_t;

/*****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/*****************************************************************************
 * Private Data
 ****************************************************************************/
PLUM_PRIVATE dx100_reg_t reg_tb[] = {
    [DEV_DX100_REG_CTL_FRE_IDX] = {0xF029, 0},
    [DEV_DX100_REG_STA_FRE_IDX] = {0xd009, 0},
    [DEV_DX100_REG_CTL_RUN_IDX] = {0x1300, 0},
    [DEV_DX100_REG_STA_RUN_IDX] = {0xD008, 0},
};

/*****************************************************************************
 * Public Data
 ****************************************************************************/

/*****************************************************************************
 * Private Functions
 ****************************************************************************/

PLUM_PRIVATE
plum_void dev_vfd_dx100_notice(modbus_protocol_evt evt, plum_void *arg)
{
    switch (evt) {
        case MODBUS_EVT_MSG:
        {
            modbus_msg_t *msg = (modbus_msg_t *)arg;
            plum_u8       loop;
            for (loop = 0; loop < PLUM_ARRAY_CNT(reg_tb); loop++) {
                if (reg_tb[loop].addr == msg->addr) {
                    reg_tb[loop].value = *(plum_u16 *)msg->value;
                    break;
                }
            }
        } break;

        case MODBUS_EVT_ERR:
        {
            modbus_err_t *msg = (modbus_err_t *)arg;
            LOG_E("modbus err:%s", msg->discr);
        } break;

        default:
            LOG_E("dev_vfd_c10_notice err evt");
            break;
    }
}

/*****************************************************************************
 * Public Functions
 ****************************************************************************/

PLUM_PUBLIC
plum_s32 dev_vfd_run(plum_u8 id, dev_vfd_run_t type)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        plum_u16 value;
        switch (type) {
            case DEV_VFD_RUN_PST:
                value = 0x10;
                break;
            case DEV_VFD_RUN_NGT:
                value = 0x10;
                break;
            case DEV_VFD_RUN_STOP:
                value = 0;
                break;
            default:
                break;
        }
        rc = modbus_protocol_write(
            MODBUS_PROTOCOL_RTU, id, MODBUS_PROTOCOL_WRITE_SHOLD,
            reg_tb[DEV_DX100_REG_CTL_RUN_IDX].addr, &value, 2);
        if (rc) {
            LOG_E("modbus_protocol_write err,rc: %d", rc);
            break;
        }
    } while (0);

    return (rc);
}

PLUM_PUBLIC
dev_vfd_run_t dev_vfd_run_get(plum_u8 id)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        rc = modbus_protocol_register_notice(dev_vfd_dx100_notice);
        if (rc) {
            LOG_E("modbus_protocol_register_notice err,rc: %d", rc);
            break;
        }

        rc = modbus_protocol_read(MODBUS_PROTOCOL_RTU, id,
                                  MODBUS_PROTOCOL_READ_SHOLD,
                                  reg_tb[DEV_DX100_REG_STA_RUN_IDX].addr, 1);
        if (rc) {
            LOG_E("modbus_protocol_read err,rc: %d", rc);
            break;
        }

        if (reg_tb[DEV_DX100_REG_STA_RUN_IDX].value & 0x01) {
            rc = DEV_VFD_RUN_STOP;
        }

        switch ((reg_tb[DEV_DX100_REG_STA_RUN_IDX].value >> 4) & 0x0F) {
#if 0
            case 0x10:
                rc = DEV_VFD_RUN_PST;
                break;
            case 0x11:
                rc = DEV_VFD_RUN_NGT;
                break;
#endif

            case 0x00:
                rc = DEV_VFD_RUN_STOP;
                break;
            default:
                rc = DEV_VFD_RUN_PST;
                break;
        }
    } while (0);

    return (rc);
}

plum_s32 dev_vfd_fre(plum_u8 id, plum_u32 fre)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        plum_u16 value = fre * 100;

        rc = modbus_protocol_write(
            MODBUS_PROTOCOL_RTU, id, MODBUS_PROTOCOL_WRITE_SHOLD,
            reg_tb[DEV_DX100_REG_CTL_FRE_IDX].addr, &value, 2);
        if (rc) {
            LOG_E("modbus_protocol_write err,rc: %d", rc);
            break;
        }
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_u32 dev_vfd_fre_get(plum_u8 id)
{
    plum_u32 freq = 0;

    do {
        plum_s32 rc = modbus_protocol_register_notice(dev_vfd_dx100_notice);
        if (rc) {
            LOG_E("modbus_protocol_register_notice err,rc: %d", rc);
            break;
        }

        rc = modbus_protocol_read(MODBUS_PROTOCOL_RTU, id,
                                  MODBUS_PROTOCOL_READ_SHOLD,
                                  reg_tb[DEV_DX100_REG_STA_FRE_IDX].addr, 1);
        if (rc) {
            LOG_E("modbus_protocol_read err,rc: %d", rc);
            break;
        }

        freq = reg_tb[DEV_DX100_REG_STA_FRE_IDX].value / 100;
    } while (0);

    return (freq);
}

/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
